package com.warmlight.voicepacket.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.CreatePlaylistActivity;
import com.warmlight.voicepacket.PlayDetailMineActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.RecordActivity;
import com.warmlight.voicepacket.TextToVoiceActivity;
import com.warmlight.voicepacket.adapter.MyPlaylistAdapter;
import com.warmlight.voicepacket.base.BaseFragment;
import com.warmlight.voicepacket.data.eventbus.NotifyMyPlaylist;
import com.warmlight.voicepacket.dbdata.DBFloatPlaylist;
import com.warmlight.voicepacket.dbmanager.DBFloatPlayListDaoHelp;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PlaylistFragment extends BaseFragment {

    private ListView lv_my_playlist;
    private MyPlaylistAdapter mMyPlaylistAdapter;
    private List<DBFloatPlaylist> mPlaylist;
    public static final String LOCAL = "local";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_playlist, container, false);

        initViews();
        queryMyList();
        EventBus.getDefault().register(this);
        return mView;
    }

    //订阅方法，当接收到事件的时候，会调用该方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(NotifyMyPlaylist data){
        queryMyList();
    }

    private void initViews(){
        lv_my_playlist = (ListView) mView.findViewById(R.id.lv_my_playlist);
        LinearLayout linearLayout = new LinearLayout(getContext());
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(params) ;
        lv_my_playlist.addHeaderView(linearLayout);
        initHead(linearLayout);
        mMyPlaylistAdapter = new MyPlaylistAdapter(getActivity().getApplicationContext(),new ArrayList<DBFloatPlaylist>());
        lv_my_playlist.setAdapter(mMyPlaylistAdapter);
        lv_my_playlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(getContext(), PlayDetailMineActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(LOCAL,(DBFloatPlaylist)mPlaylist.get(position-1));
                intent.putExtras(bundle);

                //我的播单点击
                MobclickAgent.onEvent(getActivity().getApplicationContext(), MobClickHelper.CLOT_FEED,mPlaylist.get(position-1).getList_name());
                HashMap<String,Object> temp = new HashMap<>();
                temp.put("我的播单点击",mPlaylist.get(position-1).getList_name().toString());
                ZhugeSDK.getInstance().track(getActivity().getApplicationContext(),MobClickHelper.CLOT_FEED,temp);

                getContext().startActivity(intent);
            }
        });

        findViewById(R.id.iv_create_list).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //我的播单创建
                MobclickAgent.onEvent(getActivity().getApplicationContext(), MobClickHelper.CLOT_BUILD);
                ZhugeSDK.getInstance().track(getActivity().getApplicationContext(),MobClickHelper.CLOT_BUILD);

                getActivity().startActivity(new Intent(getActivity(), CreatePlaylistActivity.class));
            }
        });
    }

    private void initHead(LinearLayout linearLayout){
        View view = View.inflate(getContext(), R.layout.header_voice_change, null);
        final LinearLayout text2voice = (LinearLayout) view.findViewById(R.id.ll_textvoice);
        LinearLayout voiceChange = (LinearLayout) view.findViewById(R.id.ll_voice_change);
        text2voice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(getActivity().getApplicationContext(), MobClickHelper.CLOT_TXTOSPEECH,"文字转语音");
                HashMap<String,Object> temp = new HashMap<>();
                temp.put("文字转语音","文字转语音");
                ZhugeSDK.getInstance().track(getActivity().getApplicationContext(),MobClickHelper.CLOT_TXTOSPEECH,temp);
                startActivity(new Intent(getContext(), TextToVoiceActivity.class));
            }
        });

        voiceChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(getActivity().getApplicationContext(), MobClickHelper.CLOT_RECORD,"变声录音");
                HashMap<String,Object> temp = new HashMap<>();
                temp.put("变声录音","变声录音");
                ZhugeSDK.getInstance().track(getActivity().getApplicationContext(),MobClickHelper.CLOT_RECORD,temp);
                startActivity(new Intent(getContext(), RecordActivity.class));
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        linearLayout.addView(view);
    }

    private void queryMyList(){
        if(mMyPlaylistAdapter!=null){
//            mPlaylist = DBHelper.getInstance(getActivity()).queryAllList();
            mPlaylist = DBFloatPlayListDaoHelp.loadAll(getActivity());
            mMyPlaylistAdapter.setList(mPlaylist);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
